package service

import (
	"github.com/astaxie/beego/orm"
	"net/url"
	"xianzhi/models"
	"xianzhi/utils"
)

// 获取订单信息
func GetOrderInfo(p map[string]interface{}) *models.Order {
	o := orm.NewOrm()
	order := &models.Order{}
	qs := o.QueryTable("Order")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["openId"]; ok {
		qs = qs.Filter("open_id", p["openId"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("phone", p["phone"])
	}
	qs.One(order)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if order.Id > 0 {
			order.CreatedShow = order.Created.Format("2006-01-02 15:04:05")
			order.CreatedDateShow = order.Created.Format("2006.01.02")
		}
	}
	return order
}

// 添加订单信息
func AddOrder(input url.Values) (int64, error) {
	o := orm.NewOrm()
	order := &models.Order{}
	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	order.User = user
	setMeal := &models.SetMeal{}
	if _, ok := input["set_meal_id"]; ok {
		setMeal.Id = utils.Atoi64(input["set_meal_id"][0])
		o.Read(setMeal)
	}
	order.SetMeal = setMeal
	if setMeal.Id > 0 {
		order.SetMealName = setMeal.Name
		order.SetMealIntroduce = setMeal.Introduce
		order.SetMealPrice = setMeal.Price
		order.SetMealPushNum = setMeal.PushNum
		order.SetMealExcellentNum = setMeal.ExcellentNum
		order.SetMealAssistNum = setMeal.AssistNum
	}
	return o.Insert(order)
}

// 修改订单信息
func EditOrder(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("Order").Filter("id", input["id"][0]).Update(p)
}

// 获取订单列表
func GetOrderList(p map[string]interface{}) (int64, []*models.Order) {
	var orderList []*models.Order
	o := orm.NewOrm()
	qs := o.QueryTable("Order")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["setMealId"]; ok {
		qs = qs.Filter("set_meal_id", p["setMealId"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("user__phone__contains", p["phone"])
	}
	if _, ok := p["realName"]; ok {
		qs = qs.Filter("user__real_name__contains", p["realName"])
	}
	if _, ok := p["nickName"]; ok {
		qs = qs.Filter("user__nick_name__contains", p["nickName"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-created").All(&orderList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, order := range orderList {
			order.CreatedShow = order.Created.Format("2006-01-02 15:04:05")
			order.CreatedDateShow = order.Created.Format("2006-01-02")
		}
	}
	return totalCount, orderList
}

func DelOrder(p map[string]interface{}) (int64, error) {
	if len(p) < 1 {
		return -1, nil
	}
	o := orm.NewOrm()
	qs := o.QueryTable("Order")
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	return qs.Delete()
}
